import tkinter as tk
import random
import math

# 全局参数
window_count = 0  # 爱心弹窗计数器
MAX_HEART_WINDOWS = 70  # 爱心弹窗总数
HEART_WINDOW_SIZE = 60  # 爱心弹窗大小
HEART_SCALE = 35  # 爱心缩放比例（不变）
RANDOM_WINDOW_SIZE = 80  # 随机弹窗大小
MAX_RANDOM_WINDOWS = 300  # 随机弹窗总数（确保铺满屏幕）
random_window_count = 0  # 随机弹窗计数器


def get_heart_points(total, scale):
    """保持爱心形状生成逻辑"""
    points = []
    for i in range(total):
        t = 2 * math.pi * i / total
        x = 14 * (math.sin(t)) ** 3
        y = 12 * math.cos(t) - 4 * math.cos(2 * t) - 2 * math.cos(3 * t) - math.cos(4 * t)
        x = int(x * scale)
        y = int(-y * scale)
        points.append((x, y))
    return points


def create_heart_tip(x, y):
    """创建爱心轮廓弹窗（四字祝福）"""
    global window_count
    if window_count >= MAX_HEART_WINDOWS:
        return

    window = tk.Toplevel(root)
    window_count += 1

    window.geometry(f"{HEART_WINDOW_SIZE}x{HEART_WINDOW_SIZE}+{x}+{y}")
    window.title('生日快乐')

    bg_colors = [
        '#FFEEEE', '#FFF0F5', '#FFF5EE', '#FEF3C7',
        '#F3E8FF', '#E0F2FE', '#DBEAFE', '#FFE8E8'
    ]
    bg = random.choice(bg_colors)

    birthday_tips = [
        '廿四欢喜', '岁喜安康', '青春正好', '笑靥如花',
        '万事顺意', '未来可期', '平安喜乐', '顺遂无忧',
        '光芒万丈', '温柔坚定', '好运加满', '喜乐长安'
    ]
    tip = random.choice(birthday_tips)

    tk.Label(
        window,
        text=tip,
        bg=bg,
        font=('微软雅黑', 12, 'bold'),
        justify='center',
        padx=5,
        pady=5
    ).pack(expand=True, fill='both')

    window.attributes('-topmost', True)
    window.lift()


def create_center_tip(x, y):
    """创建中心弹窗（弹性布局，最后出现）"""
    window = tk.Toplevel(root)
    window.minsize(width=120, height=80)
    window.geometry(f"+{x}+{y}")
    window.title('生日快乐')
    window.configure(bg='#FFB6C1')

    label = tk.Label(
        window,
        text="杨慧24岁生日快乐捏！🎂🎂",
        bg='#FFB6C1',
        font=('微软雅黑', 16, 'bold'),
        justify='center',
        padx=15,
        pady=15
    )
    label.pack(expand=True, fill='both')

    window.attributes('-topmost', True)
    window.lift()

    # 中心弹窗出现后，延迟1秒开始全屏随机弹窗
    root.after(1000, start_random_windows)


def create_random_tip(x, y):
    """创建全屏随机弹窗（内容固定为“🎂生日快乐🎂”）"""
    global random_window_count
    if random_window_count >= MAX_RANDOM_WINDOWS:
        return

    window = tk.Toplevel(root)
    random_window_count += 1

    # 随机弹窗尺寸固定，位置随机
    window.geometry(f"{RANDOM_WINDOW_SIZE}x{RANDOM_WINDOW_SIZE}+{x}+{y}")
    window.title('生日快乐')

    # 随机柔和背景色（与主题统一）
    bg_colors = [
        '#FFE4E1', '#FFF0F5', '#FEF3C7', '#E6F7FF',
        '#F0F9FF', '#FFF7E6', '#F6FFED', '#FFEEF2'
    ]
    bg = random.choice(bg_colors)

    # 固定内容
    tk.Label(
        window,
        text="🎂生日快乐🎂",
        bg=bg,
        font=('微软雅黑', 12, 'bold'),
        justify='center',
        padx=5,
        pady=5
    ).pack(expand=True, fill='both')

    window.attributes('-topmost', True)
    window.lift()


def start_random_windows():
    """生成全屏随机弹窗，铺满整个屏幕"""
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()

    # 计算随机坐标范围（确保弹窗完全在屏幕内）
    max_x = screen_width - RANDOM_WINDOW_SIZE
    max_y = screen_height - RANDOM_WINDOW_SIZE

    # 依次生成随机弹窗（间隔50ms，逐渐铺满屏幕）
    for i in range(MAX_RANDOM_WINDOWS):
        # 随机生成坐标（避免边缘，留10px边距）
        x = random.randint(10, max_x - 10)
        y = random.randint(10, max_y - 10)
        # 按顺序弹出，形成动态铺满效果
        root.after(i * 50, create_random_tip, x, y)


def arrange_animation():
    """整体动画流程：爱心弹窗→中心弹窗→随机弹窗"""
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
    center_x = screen_width // 2
    center_y = screen_height // 2

    # 1. 先生成爱心轮廓弹窗
    heart_points = get_heart_points(MAX_HEART_WINDOWS, HEART_SCALE)
    for i, (x, y) in enumerate(heart_points):
        window_x = center_x + x - HEART_WINDOW_SIZE // 2
        window_y = center_y + y - HEART_WINDOW_SIZE // 2
        window_x = max(10, min(window_x, screen_width - HEART_WINDOW_SIZE - 10))
        window_y = max(10, min(window_y, screen_height - HEART_WINDOW_SIZE - 10))
        root.after(i * 100, create_heart_tip, window_x, window_y)

    # 2. 爱心弹窗完成后，弹出中心弹窗（延迟计算：爱心弹窗总时长+500ms）
    center_delay = MAX_HEART_WINDOWS * 100 + 500
    expected_width = 120
    expected_height = 80
    center_window_x = center_x - expected_width // 2
    center_window_y = center_y - expected_height // 2
    center_window_x = max(10, min(center_window_x, screen_width - expected_width - 10))
    center_window_y = max(10, min(center_window_y, screen_height - expected_height - 10))
    root.after(center_delay, create_center_tip, center_window_x, center_window_y)


# 主窗口初始化
root = tk.Tk()
root.withdraw()  # 隐藏主窗口

# 启动完整动画流程
arrange_animation()

root.mainloop()